﻿using MySqlConnector;
using SqlSugar;
using 档案查询系统网络版.Models;

namespace 档案查询系统网络版.Helpers
{
    public static class AddSqlsugar
    {
        public static void AddSqlsugarSetup(this IServiceCollection services,IConfiguration configuration,string dbName = "Mysql")
        {
            SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
            {
                ConnectionString = configuration.GetConnectionString(dbName),
                DbType = DbType.MySql,
                IsAutoCloseConnection = true
            },
            db=>
            {
                // 单例参数配置，所有上下文生效
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    Console.WriteLine(sql + "\r\n" + pars);// 输出sql语句及参数
                };
            });
            services.AddSingleton<ISqlSugarClient>(sqlSugar);
        }
        
        public static void InitDb(ISqlSugarClient db)
        {
            db.DbMaintenance.CreateDatabase();
            db.CodeFirst.InitTables(typeof(User));
            db.CodeFirst.InitTables(typeof(SystemLog));
        }
    }
}
